// hyb_yl/pharmacy/pages/pharmacy_cont/pharmacy_cont.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    keyword:"",
    type:"",
    page:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var agentname = options.agentname;
    var hid = options.hid;
    var that = this;
    that.setData({
      agentname:agentname,
      hid:hid
    })
    wx.setNavigationBarTitle({
      title: agentname,
    })
    
  },
  topingjia:function(){
    var that = this;
    
  },
  // 获取药房详情
  getHospital:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/Goods.hospital_detail",
      data:{
        hid:that.data.hid
      },
      success:function(res){
        that.setData({
          info:res.data
        })
      }
    })
  },
  // 获取药品列表
  getList:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/goods.goods_list",
      data:{
        hid:that.data.hid,
        page:that.data.page,
        type:that.data.type,
        keyword:that.data.keyword
      },
      success:function(res){
        var page = that.data.page + 1;
        that.setData({
          list:that.data.list.concat(res.data),
          page:page
        })
        that.getcar();
      }
    })
  },
  // 获取购物车列表
  getcar: function () {
    var that = this;
    var list = that.data.list;
    var carlist = wx.getStorageSync('shopcar');
    for (var i = 0; i < list.length; i++) {
      for (var j = 0; j < carlist.length; j++) {
        if (list[i].sid == carlist[j]['sid']) {
          list[i]['num'] = carlist[j]['num']
        }
      }

    }
    that.setData({
      list: list,
    })
  },
  // 跳转详情和评价
  toshop:function(e){
    var that = this;
    wx.navigateTo({
      url: '/hyb_yl/pharmacy/pages/shop_list/shop_list?hid='+that.data.hid+"&agentname="+that.data.agentname,
    })
  },
  // 跳转药品详情
  todetail:function(e){
    var that =this;
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/product/product?sid='+e.currentTarget.dataset.sid,
    })
  },
  // 获取药品分类
  getType: function () {
    var that = this;
    app.util.request({
      url: "entry/wxapp/goods.goods_type",
      success: function (res) {
        console.log(res)
        var type_list = res.data.lists
        var type_lists = res.data.list
        that.setData({
          type_list: type_list,
          type_lists:type_lists
        })
      },
    });
  },
  // 修改关键词
  changekeyword:function(e){
    var that = this;
    var keyword = e.detail.value;
    console.log(e)
    that.setData({
      keyword:keyword,
      
    })
  },
  // 关键词搜索
  searchs:function(res){
    var that = this;
    that.setData({
      page:0,
      list:[],
    })
    that.getList();
  },
  // 类型修改
  changetype:function(e){
    var that = this;
    var type = e.currentTarget.dataset.id;
    that.setData({
      type:type,
      page:0,
      list:[]
    })
    that.getList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      page:0,
      list:[]
    })
    that.getHospital();
    that.getList();
    that.getType();
    var carlist = wx.getStorageSync('shopcar')
    that.setData({
      carlist: carlist
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})